<?php

namespace app\api\controller;

use app\admin\model\post\Comment;
use app\admin\model\post\Reward;
use app\common\controller\Api;

use app\common\library\Email;
use Carbon\Carbon;
use fast\Date;
use think\Config;
use think\Db;
use think\Exception;
use think\helper\Time;

/**
 * 圈子
 */
class Post extends Api
{
    protected $noNeedLogin = [];
    protected $noNeedRight = '*';

    protected $field=['content','images'];


    public function create(){
        $params=$this->getParams($this->field);
        $params['uid']=$this->uid();
        if(empty($params['content'])) throw new Exception('内容不能为空');
        $player=\app\admin\model\user\Player::get(['uid'=>$params['uid']]);
        if(empty($player) || $player->status != 1 ) throw new Exception('身份错误');
        $params['createtime']=date('Y-m-d H:i:s');
        $result=\app\admin\model\Post::create($params);
        $this->success('',$result);
    }


    public function list(){
        $page=$this->request->request('page',1);
        $list = \app\admin\model\Post::with(['user'=>function($query){
            $query->withField('id,nickname,avatar,gender,birthday,bio');
        }])->where(['post.status'=>1])->page($page)->order('id desc')->select();
        foreach ($list as $item){
            $item['images'] = explode(',',$item['images']);
            $item['comment_num']=Comment::where(['post_id'=>$item['id'],'reply_id'=>0])->count();
            $item['reward_num']=Comment::where(['post_id'=>$item['id']])->count();
            $item['is_like'] = Db::name('post_like')->where(['uid'=>$this->uid(),'post_id'=>$item['id']])->find() ? 1 : 0 ;

        }
        $this->success('',$list);
    }

    public function myList(){
        $page=$this->request->request('page',1);
        $list = \app\admin\model\Post::with(['user'=>function($query){
            $query->withField('id,nickname,avatar,gender,birthday,bio');
        }])->where(['post.status'=>1,'uid'=>$this->uid()])->page($page)->order('id desc')->select();
        foreach ($list as $item){
            $item['images'] = explode(',',$item['images']);
            $item['comment_num']=Comment::where(['post_id'=>$item['id'],'reply_id'=>0])->count();
            $item['reward_num']=Comment::where(['post_id'=>$item['id']])->count();
            $item['is_like'] = Db::name('post_like')->where(['uid'=>$this->uid(),'post_id'=>$item['id']])->find() ? 1 : 0 ;

        }
        $this->success('',$list);
    }
    public function del(){
        $post_id=$this->request->post('post_id');
        $res=\app\admin\model\Post::destroy(['id'=>$post_id,'uid'=>$this->uid()]);
        Db::name('post_like')->where(['post_id'=>$post_id])->delete();
    }
    public function like(){
        $post_id=$this->request->post('post_id');
        if(!\app\admin\model\Post::get($post_id)) throw new Exception('圈子不存在');
        $post=Db::name('post_like');
        if(!$post->where(['uid'=>$this->uid(),'post_id'=>$post_id])->find()){
            $r=$post->insert(['uid'=>$this->uid(),'post_id'=>$post_id,'createtime'=>Carbon::now()->toDateTimeString()]);
            \app\admin\model\Post::where(['id'=>$post_id])->setInc('like_num');
        }
        $this->success('',$r ?? 0);
    }

    public function likeList(){
        $post_id=$this->request->get('post_id');
        $page=$this->request->get('page',1);
        $list=Db::name('post_like')->where('post_id',$post_id)->page($page)->select();
        foreach ($list as &$item){
            $item['user']=\app\admin\model\User::where(['id'=>$item['uid']])->field('nickname,avatar')->find();
        }
        $this->success('',$list);
    }


    public function reward(){
        $post_id = $this->request->post('post_id');
        $amount = $this->request->post('amount');
        $result = (new Reward())->reward($post_id,$amount,$this->uid());
        $this->success('',$result);

    }

     public function rewardList(){
        $post_id=$this->request->get('post_id');
        $page=$this->request->get('page',1);
        $list=Reward::where(['post_id'=>$post_id,'status'=>2])->page($page)->select();
        foreach ($list as &$item){
            $item['user']=\app\admin\model\User::where(['id'=>$item['uid']])->field('nickname,avatar')->find();
        }
        $this->success('',$list);
     }

}
